package com.dotmedia.dboperations;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dotmedia.core.DatabaseAccessService;
import com.dotmedia.entities.Permission;
import com.dotmedia.entities.Role;
import com.dotmedia.entities.RolePermission;
import com.dotmedia.entities.SystemEntity;

/**
 * This class is the database operation class for the RolePermission entity
 * 
 * @author siddharthsaha
 * 
 */
public class RolePermissionDatabaseOperations {

	private static Logger logger = Logger.getLogger("RolePermissionDatabaseOperations");

	/**
	 * This method persists an entity of RolePermission
	 * 
	 * @param rolePermissionEntity
	 *        The RolePermission entity which is to be persisted in the database
	 */
	public void persistRolePermissionEntity(RolePermission rolePermissionEntity) {
		logger.info("IN method persistRolePermissionEntity() in RolePermissionDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int persistStatus = databaseAccessService.persistEntity(rolePermissionEntity);
		if (persistStatus == 0) {
			logger.info("The entity of RolePermission was successfully persisted. Id: " + rolePermissionEntity.getId());
		} else {
			logger.error("The entity of RolePermission cannot be successfully persisted. Id: " + rolePermissionEntity.getId());
		}
		logger.info("OUT method persistRolePermissionEntity() in RolePermissionDatabaseOperations");
	}

	/**
	 * Removes a role permission entity from the database
	 * 
	 * @param rolePermissionEntity
	 *        The entity of RolePermission which is to be removed from the database
	 */
	public void removeRolePermissionEntity(RolePermission rolePermissionEntity) {
		logger.info("IN method removeRolePermissionEntity() in RolePermissionDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int removeStatus = databaseAccessService.removeEntity(rolePermissionEntity, rolePermissionEntity.getId());
		if (removeStatus == 0) {
			logger.info("The RolePermission entity was successfully removed from the database. Id: " + rolePermissionEntity.getId());
		} else {
			logger.error("The RolePermission entity cannot be successfully removed from the database. Id: " + rolePermissionEntity.getId());
		}
		logger.info("OUT method removeRolePermissionEntity() in RolePermissionDatabaseOperations");
	}

	/**
	 * Updates a RolePermission entity in the database
	 * 
	 * @param rolePermissionEntity
	 *        The RolePermission entity which is to be updated
	 */
	public void updateRolePermissionEntity(RolePermission rolePermissionEntity) {
		logger.info("IN method updateRolePermissionEntity() in RolePermissionDatabaseOperations");
		String query = "SELECT rolePermissionEntity FROM RolePermission rolePermissionEntity where rolePermissionEntity.id = "
				+ rolePermissionEntity.getId();
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List<RolePermission> retrievedEntities = databaseAccessService.getEntities(query);
		boolean obtained = false;
		for (int retrievedEntitiesIndex = 0; retrievedEntitiesIndex < retrievedEntities.size(); retrievedEntitiesIndex++) {
			RolePermission currentRolePermissionEntity = retrievedEntities.get(retrievedEntitiesIndex);
			if (currentRolePermissionEntity.getId().intValue() == rolePermissionEntity.getId().intValue()) {
				obtained = true;
				logger.info("Got the RolePermission entity which is to be updated");
				currentRolePermissionEntity.setRole(rolePermissionEntity.getRole());
				currentRolePermissionEntity.setPermission(rolePermissionEntity.getPermission());
				currentRolePermissionEntity.setSystem(rolePermissionEntity.getSystem());
				int updateStatus = databaseAccessService.updateEntity(currentRolePermissionEntity);
				if (updateStatus == 0) {
					logger.info("Updated the RolePermission entity. Id: " + rolePermissionEntity.getId());
				} else {
					logger.error("Error while updating the RolePermission entity. Id: " + rolePermissionEntity.getId());
				}
			}
		}
		if (!obtained) {
			logger.warn("Could not get RolePermission entity to update. The searched id: " + rolePermissionEntity.getId());
		}
		logger.info("OUT method updateRolePermissionEntity() in RolePermissionDatabaseOperations");
	}

	/**
	 * This method gets a list of RolePermission entities which are of a Role passed to it
	 * 
	 * @param roleEntity
	 *        The Role entity
	 * @return
	 */
	public List<RolePermission> getRolePermissionEntitiesWithRole(Role roleEntity) {
		logger.info("IN method getRolePermissionEntitiesWithRole() in RolePermissionDatabaseOperations");
		List<RolePermission> toReturnEntities = new ArrayList<RolePermission>();
		String query = "SELECT rolePermissionEntity FROM RolePermission rolePermissionEntity WHERE rolePermissionEntity.role.id = "
				+ roleEntity.getId();
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int retrievedEntitiesIndex = 0; retrievedEntitiesIndex < retrievedEntities.size(); retrievedEntitiesIndex++) {
			RolePermission currentRolePermissionEntity = (RolePermission) retrievedEntities.get(retrievedEntitiesIndex);
			toReturnEntities.add(currentRolePermissionEntity);
		}
		logger.info("OUT method getRolePermissionEntitiesWithRole() in RolePermissionDatabaseOperations");
		return toReturnEntities;
	}

	/**
	 * This method returns a list of RolePermission entities which are of a Permission passed to it.
	 * 
	 * @param permissionEntity
	 *        The Permission entity
	 * @return
	 */
	public List<RolePermission> getRolePermissionEntitiesWithPermission(Permission permissionEntity) {
		logger.info("IN method getRolePermissionEntitiesWithPermission() in RolePermissionDatabaseOperations");
		List<RolePermission> toReturnList = new ArrayList<RolePermission>();
		String query = "SELECT rolePermissionEntity FROM RolePermission rolePermissionEntity WHERE rolePermissionEntity.permission.id = "
				+ permissionEntity.getId();
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int retrievedEntitiesIndex = 0; retrievedEntitiesIndex < retrievedEntities.size(); retrievedEntitiesIndex++) {
			RolePermission currentRolePermission = (RolePermission) retrievedEntities.get(retrievedEntitiesIndex);
			toReturnList.add(currentRolePermission);
		}
		logger.info("OUT method getRolePermissionEntitiesWithPermission() in RolePermissionDatabaseOperations");
		return toReturnList;
	}

	/**
	 * 
	 * @param systemEntity
	 * @return
	 */
	public List<RolePermission> getRolePermissionEntitiesWithSystem(SystemEntity systemEntity) {
		logger.info("IN method getRolePermissionEntitiesWithSystem() in RolePermissionDatabaseOperations");
		List<RolePermission> toReturnList = new ArrayList<RolePermission>();
		String query = "SELECT rolePermissionEntity FROM RolePermission rolePermissionEntity WHERE rolePermissionEntity.system.id = "
				+ systemEntity.getId();
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retreivedEntities = databaseAccessService.getEntities(query);
		for (int retrievedEntitiesIndex = 0; retrievedEntitiesIndex < retreivedEntities.size(); retrievedEntitiesIndex++) {
			RolePermission currentRolePermission = (RolePermission) retreivedEntities.get(retrievedEntitiesIndex);
			toReturnList.add(currentRolePermission);
		}
		logger.info("OUT method getRolePermissionEntitiesWithSystem() in RolePermissionDatabaseOperations");
		return toReturnList;
	}

	/**
	 * Gets the list of RolePermission entities of a given Role and a given Permission
	 * 
	 * @param roleEntity
	 *        The Role entity
	 * @param permissionEntity
	 *        The Permission Entity
	 * @return
	 */
	public List<RolePermission> getRolePermissionEntitiesWithRoleAndPermission(Role roleEntity, Permission permissionEntity) {
		logger.info("IN method getRolePermissionEntitiesWithRoleAndPermission() in RolePermissionDatabaseOperations");
		List<RolePermission> toReturnList = new ArrayList<RolePermission>();
		String query = "SELECT rolePermissionEntity FROM RolePermission rolePermissionEntity WHERE rolePermisisonEntity.role.id = "
				+ roleEntity.getId() + " AND rolePermissionEntity.permission.id = " + permissionEntity.getId();
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int retrievedEntitiesIndex = 0; retrievedEntitiesIndex < retrievedEntities.size(); retrievedEntitiesIndex++) {
			RolePermission currentRolePermission = (RolePermission) retrievedEntities.get(retrievedEntitiesIndex);
			toReturnList.add(currentRolePermission);
		}
		logger.info("OUT method getRolePermissionEntitiesWithRoleAndPermission() in RolePermissionDatabaseOperations");
		return toReturnList;
	}

}
